package com.wejoy.video.middle.station.data.service.system.impl;

import com.wejoy.video.middle.station.data.domain.system.SysDept;
import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.system.ISysCarmeraMapper;
import com.wejoy.video.middle.station.data.domain.system.SysCarmera;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.system.ISysCarmeraService;
import com.wejoy.video.middle.station.data.service.system.ISysDeptService;
import com.wejoy.video.middle.station.data.util.StringUtils;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 点位
 *
 * @author miracle_j
 * @date 2024年04月07日 18:21:43
 */
@Service
public class SysCarmeraServiceImpl implements ISysCarmeraService {
    @Autowired
    private ISysCarmeraMapper sysCarmeraMapper;
    @Autowired
    private IIdWorkerService idWorkerService;
    @Autowired
    private ISysDeptService sysDeptService;

    @Override
    public void save(SysCarmera req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(SysCarmera req) {
        if(req.getId()==null){
            req.setId(idWorkerService.getNextId());
        }
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return sysCarmeraMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(SysCarmera sysCarmera) {
        WJAssert.notNull(sysCarmera, "入参对象不能为空");
        WJAssert.notNull(sysCarmera.getId(), "id参数错误");
        SysCarmera where = new SysCarmera();
        where.setId(sysCarmera.getId());
        SysCarmera oneById = getOneById(sysCarmera.getId());
        SysDept sysDept = sysDeptService.getOneById(oneById.getDepId());
        if(!StringUtils.isBlank(sysDept.getCameraType())){
            List<Integer> list = Arrays.stream(sysDept.getCameraType().split(","))
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
            //是否包含sysCarmera.getCameraType()
            if(sysCarmera.getCameraType()!=null){
                boolean contains = list.contains(Integer.valueOf(sysCarmera.getCameraType()));
                if(!contains){
                    list.add(sysCarmera.getCameraType());
                    String result = list.stream()
                            .map(String::valueOf)
                            .collect(Collectors.joining(","));
                    sysDept.setCameraType(result);
                    sysDeptService.updateSelectiveById(sysDept);
                }
            }
            if(sysCarmera.getCameraMiniType()!=null){
                boolean contains = list.contains(Integer.valueOf(sysCarmera.getCameraMiniType()));
                if(!contains){
                    list.add(sysCarmera.getCameraMiniType());
                    String result = list.stream()
                            .map(String::valueOf)
                            .collect(Collectors.joining(","));
                    sysDept.setCameraType(result);
                    sysDeptService.updateSelectiveById(sysDept);
                }
            }
        }
        return sysCarmeraMapper.updateSelective(sysCarmera, where);
    }

    @Override
    public int getCount(SysCarmera sysCarmera) {
        checkStringLength(sysCarmera, true);
        return sysCarmeraMapper.getCount(sysCarmera);
    }

    @Override
    public SysCarmera getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        SysCarmera where = new SysCarmera();
        where.setId(id);
        return sysCarmeraMapper.getOne(where);
    }

    @Override
    public int getAlarmCount(SysCarmera sysCarmera) {
        return sysCarmeraMapper.getAlarmCountByType(sysCarmera);
    }

    @Override
    public List<SysCarmera> getList(SysCarmera sysCarmera) {
        checkStringLength(sysCarmera, true);
        return sysCarmeraMapper.getList(sysCarmera);
    }

    @Override
    public List<SysCarmera> getPageList(SysCarmera sysCarmera) {
        checkStringLength(sysCarmera, true);
        return sysCarmeraMapper.getPageList(sysCarmera);
    }

    @Override
    public void audit(Long id, String operator) {
        SysCarmera req = new SysCarmera();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        SysCarmera req = new SysCarmera();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        SysCarmera req = new SysCarmera();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        sysCarmeraMapper.delete(id);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(SysCarmera sysCarmera, boolean nullAble) {
        WJAssert.notNull(sysCarmera, "入参对象不能为空");
        WJAssert.limitMaxLength(sysCarmera.getAiTypes(), true, 150, "AI种类不合法!");
        WJAssert.limitMaxLength(sysCarmera.getOperator(), true, 100, "操作人不合法!");
        WJAssert.limitMaxLength(sysCarmera.getRemark(), true, 255, "备注不合法!");
    }
}
